import {
  Body,
  Controller,
  Get,
  Post,
  Render,
  Request,
  Response,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { AppService } from './app.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { multerConfig } from 'src/config/multerConfig';
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  @Render('default/index')
  getHello() {
    return {
      name: '王五',
    };
  }
  @Get('aticle')
  aticle(@Response() res) {
    res.cookie('username', '我是cookie1', {
      maxAge: 1000 * 60 * 10,
      httpOnly: true,
      signed: false,
    });
    res.send('这是文章');
    // return '这是文章';
  }

  @Get('setSession')
  setSession(@Request() req) {
    req.session.username = '哈哈哈';
    return '设置';
  }

  @Get('getSession')
  getSession(@Request() req) {
    console.log('req.session.username', req.session.username);
    return '获取';
  }

  @Get('upload')
  @Render('default/upload')
  shangchuan() {}

  @Post('upload/doAdd')
  @UseInterceptors(FileInterceptor('file'))
  doAdd(@Body() body, @UploadedFile() file) {
    if (!file) {
      return '图片类型不对';
    }
    return {
      code: 200,
      data: {
        url: `http://localhost:3000/static/${file.filename}`,
      },
      message: 'success',
    };
  }
}
